//
//  FeelingViewController.m
//  网络FM
//
//  Created by dlios on 15/5/23.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "FeelingViewController.h"
#import "FeelingTableViewCell.h"

#import <AFNetworking.h>
#import <UIImageView+WebCache.h>

#import "SearchAnchorDetailModel.h"

#import "PlayerViewController.h"

@interface FeelingViewController ()

@end

@implementation FeelingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.choiceDetailArray = [NSMutableArray array];
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)loadView
{
    [super loadView];
   // [self createTableView];
    [self  SearchConnetion];
    self.navigationController.navigationBar.translucent = NO;
}


- (void)createTableView
{
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.table.dataSource = self;
    self.table.rowHeight = self.view.frame.size.width / 4 + 20;
    self.table.delegate = self;
    [self.view addSubview:self.table];
    [self.table release];
}



//UITableView的两个必须实现的协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionr{
    return self.choiceDetailArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentify = @"cell";
    FeelingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[FeelingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
    
    }
    SearchAnchorDetailModel *model = [self.choiceDetailArray objectAtIndex:indexPath.row];
    
   [cell.image sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:nil options:SDWebImageRetryFailed];
    cell.FMName.text = model.title;
    cell.anchorName.text = model.speak;
    cell.listen_count.text =[NSString stringWithFormat:@"%ld",model.favnum];
    return cell;
}





//具体心情界面下的网络请求
- (void)SearchConnetion
{
   // self.searchAncherDetailArray = [NSMutableArray array];
    
    //choiceDetailcell的网络请求
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"http://bapi.xinli001.com/fm2/broadcast_list.json/?offset=0&v=1&key=c605710a2343f6da4ac22c8cce194bd0&rows=15&tag="];
    NSString *s = [NSString stringWithFormat:@"%@%@",str,self.choice];
    
    
    s = [s stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
   
    [manager GET:s parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic  in array) {
            SearchAnchorDetailModel *model = [[SearchAnchorDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.choiceDetailArray addObject:model];
            
        }
        [self createTableView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    
}



//点击cell,跳转到播放界面,把model传过去
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *player = [[PlayerViewController alloc]init];
    
    
    SearchAnchorDetailModel *model = [self.choiceDetailArray objectAtIndex:indexPath.row];
    
    player.urlForPlay = model.url;
    
    [self presentViewController:player animated:YES completion:nil];
}

@end
